package com.hzw.saas.service.old.storage.service;

import java.util.Objects;

import com.hzw.saas.api.old.storage.ICatalogService;
import com.hzw.saas.api.old.storage.InxChunkService;
import com.hzw.saas.api.old.storage.bo.InxChunkBO;
import com.hzw.saas.api.old.storage.bo.ResourceFileBO;
import com.hzw.saas.common.util.enums.ConflictModeEnum;
import com.hzw.saas.service.old.hub.pojo.FileStorage;
import com.hzw.saas.service.old.hub.pojo.InxChunkMetadata;
import com.hzw.saas.service.old.hub.pojo.InxChunkStorage;
import com.hzw.saas.service.old.hub.service.IChunkStorageService;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;


/**
 * @author zzl
 * @since 04/24/2021
 */
@Service("pInxChunkService")
@RequiredArgsConstructor
@Slf4j
public class OldInxChunkServiceImpl implements InxChunkService {

    private final MapperFacade mapperFacade;
    private final ICatalogService catalogService;
    private final IChunkStorageService chunkStorageService;

    @Override
    public InxChunkBO getChunk(String catalog, String fileId, long totalChunks, long chunkNumber) {
        return null;
    }

    @Override
    public boolean existChunk(String catalog, InxChunkBO chunkInfo) {
        if (StrUtil.isNotBlank(chunkInfo.getDigest())) {
            String chunkDigest = chunkStorageService.getChunkDigest(chunkInfo.getDigestType(), chunkInfo.getFileId(),
                String.valueOf(chunkInfo.getChunkNumber()), catalogService.getCachePath(catalog));
            return Objects.equals(chunkDigest, chunkInfo.getDigest());
        }
        if (chunkInfo.getCurrentChunkSize() != null) {
            long chunkLength = chunkStorageService.getChunkLength(chunkInfo.getFileId(),
                String.valueOf(chunkInfo.getChunkNumber()), catalogService.getCachePath(catalog));
            return Objects.equals(chunkLength, chunkInfo.getCurrentChunkSize());
        }
        return chunkStorageService.existsChunk(chunkInfo.getFileId(), String.valueOf(chunkInfo.getChunkNumber()),
            catalogService.getCachePath(catalog));

    }

    @Override
    public void writeChunkMetadata(String catalog, ResourceFileBO fileInfo) {
        InxChunkMetadata inxChunkMetadata = new InxChunkMetadata();
        inxChunkMetadata.setFileId(fileInfo.getFileId())
            .setFileName(fileInfo.getName())
            .setSuperPath(catalogService.getFilePath(fileInfo.getCatalog()))
            .setLength(fileInfo.getLength())
            .setDigest(fileInfo.getDigest());
        chunkStorageService.writeChunkMetadata(inxChunkMetadata, fileInfo.getFileId(), catalogService.getCachePath(catalog));
    }

    @Override
    public InxChunkBO saveChunk(String catalog, InxChunkBO chunkInfo, byte[] data) {
        InxChunkStorage chunkStorage = mapperFacade.map(chunkInfo, InxChunkStorage.class);
        chunkStorage.setSuperPath(catalogService.getCachePath(catalog));
        chunkStorageService.saveChunk(chunkStorage, data);
        return chunkInfo;
    }

    @Override
    public void deleteChunk(String catalog, String fileId, long chunkNumber) {
        chunkStorageService.deleteChunk(fileId, String.valueOf(chunkNumber), catalogService.getCachePath(catalog));
    }

    @Override
    public void deleteChunks(String catalog, String fileId) {
        chunkStorageService.deleteChunks(fileId, catalogService.getCachePath(catalog));
    }

    @Override
    public void mergeChunks(ResourceFileBO fileInfo, String chunkCatalog, ConflictModeEnum mode) {
        FileStorage fileStorage = new FileStorage();
        fileStorage.setFileName(fileInfo.getName())
            .setSuperPath(catalogService.getFilePath(fileInfo.getCatalog()))
            .setLength(fileInfo.getLength())
            .setDigest(fileInfo.getDigest())
            .setDigestType(fileInfo.getDigest());
        chunkStorageService.mergeFileChunks(fileStorage, catalogService.getCachePath(chunkCatalog), fileInfo.getFileId(), true, mode);
    }
}
